new Vue({
    el: '#app',
    data() {
        return {
            weekdays: ['日', '一', '二', '三', '四', '五', '六'],
            currentDate: new Date(),
            today: new Date(),
            checkedDates: [],
            Personal_Name: ''
        }
    },
    computed: {
        currentYear() {
            return this.currentDate.getFullYear();
        },
        currentMonth() {
            return this.currentDate.getMonth();
        },
        currentMonthDays() {
            return new Date(this.currentYear, this.currentMonth + 1, 0).getDate();
        },
        calendarDays() {
            const year = this.currentYear;
            const month = this.currentMonth;

            const firstDay = new Date(year, month, 1);
            const lastDay = new Date(year, month + 1, 0);
            const firstDayOfWeek = firstDay.getDay();
            const prevMonthLastDay = new Date(year, month, 0).getDate();

            const daysInMonth = lastDay.getDate();
            const days = [];

            for (let i = firstDayOfWeek - 1; i >= 0; i--) {
                const date = new Date(year, month - 1, prevMonthLastDay - i);
                days.push(this.createDayObject(date, false));
            }

            for (let i = 1; i <= daysInMonth; i++) {
                const date = new Date(year, month, i);
                days.push(this.createDayObject(date, true));
            }

            const remainingDays = 42 - days.length;
            for (let i = 1; i <= remainingDays; i++) {
                const date = new Date(year, month + 1, i);
                days.push(this.createDayObject(date, false));
            }

            return days;
        },
        checkedCount() {
            return this.checkedDates.filter(date => {
                const d = new Date(date);
                return d.getFullYear() === this.currentYear &&
                    d.getMonth() === this.currentMonth;
            }).length;
        },
        progressPercentage() {
            return (this.checkedCount / this.currentMonthDays * 100) + '%';
        }
    },
    mounted() {

        let ls_URL = window.location.href;
        let ID_Card_Number = getQueryObject(ls_URL, "ID_Card_Number");
        this.Personal_Name = getQueryObject(ls_URL, "Personal_Name");


        this.GetCheckedDates(ID_Card_Number);
    },
    methods: {
        goBack() {
            window.history.back();
        },
        GetCheckedDates(ID_Card_Number) {
            // return []
            // return  [
            //      "2025-07-03",
            //     "2025-08-03",
            //     "2025-08-04",
            //     "2025-08-11",
            //     "2025-08-18",
            //     "2025-08-19",
            //     "2025-08-20",
            //     "2025-08-21",
            //     "2025-08-14",
            //     "2025-08-07",
            //     "2025-08-15",
            //     "2025-08-08",
            //     "2025-08-01"
            // ]
            // console.log("1");

            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                method: "post",
                data: {
                    SQLSerialNumber: "SQL_Checkrili", //sql名
                    Condition: "", //不必选的条件
                    ExtensionCondition: window.btoa(
                        unescape(
                            encodeURIComponent(" and personal_information.ID_Card_Number='" + ID_Card_Number + "'"))),
                },
                success: (res) => {
                    console.log(res);
                    let ldates = res.map(date => date.ddate);
                    console.log(ldates);
                    this.checkedDates = res.map(date => date.ddate);
                    // return ldates;
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
            })


        },
        createDayObject(date, isCurrentMonth) {
            return {
                date,
                isCurrentMonth,
                isToday: this.isToday(date),
                checked: this.isChecked(date)
            };
        },
        isToday(date) {
            return (
                date.getDate() === this.today.getDate() &&
                date.getMonth() === this.today.getMonth() &&
                date.getFullYear() === this.today.getFullYear()
            );
        },
        isChecked(date) {
            return this.checkedDates.some(
                d => new Date(d).toDateString() === date.toDateString()
            );
        },
        toggleCheck(date) {
            if (!date.isCurrentMonth || date > this.today) return;

            const dateString = date.toISOString().split('T')[0];
            const index = this.checkedDates.findIndex(
                d => new Date(d).toDateString() === date.toDateString()
            );

            if (index === -1) {
                this.checkedDates.push(dateString);
            } else {
                this.checkedDates.splice(index, 1);
            }

            localStorage.setItem('checkedDates', JSON.stringify(this.checkedDates));
        },
        prevMonth() {
            this.currentDate = new Date(
                this.currentYear,
                this.currentMonth - 1,
                1
            );
        },
        nextMonth() {
            this.currentDate = new Date(
                this.currentYear,
                this.currentMonth + 1,
                1
            );
        }
    }
});